﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MpSoft.Library.DTO;
using MpSoft.Basic.DataAccess;

namespace MpSoft.Library.DAO.Customers
{
    /// <summary>
    /// This object represents the methods of a Poll.
    /// </summary>
    internal class PollDAO
    {

        #region general
        private static Poll LoadObj(IDataReader datareader)
        {
            Poll obj = new Poll();
            obj.PollID = MpDataHelp.GetInt(datareader, "PollID");
            obj.Name = MpDataHelp.GetString(datareader, "Name");
            obj.Published = MpDataHelp.GetBoolean(datareader, "Published");
            obj.DisplayOrder = MpDataHelp.GetInt(datareader, "DisplayOrder");

            return obj;
        }
        #endregion

        public static int Poll_Add(Poll obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Poll_Add");
            db.AddOutParameter(dbCommand, "PollID", DbType.Int32, obj.PollID);
            db.AddInParameter(dbCommand, "Name", DbType.String, obj.Name);
            db.AddInParameter(dbCommand, "Published", DbType.Boolean, obj.Published);
            db.AddInParameter(dbCommand, "DisplayOrder", DbType.Int32, obj.DisplayOrder);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return Convert.ToInt32(db.GetParameterValue(dbCommand, "@PollID"));
            return -1;
        }
        public static int Poll_Update(Poll obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Poll_Update");
            db.AddInParameter(dbCommand, "PollID", DbType.Int32, obj.PollID);
            db.AddInParameter(dbCommand, "Name", DbType.String, obj.Name);
            db.AddInParameter(dbCommand, "Published", DbType.Boolean, obj.Published);
            db.AddInParameter(dbCommand, "DisplayOrder", DbType.Int32, obj.DisplayOrder);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static int Poll_Delete(int PollID)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Poll_Delete"); 
            db.AddInParameter(dbCommand, "PollID", DbType.Int32, PollID);
           
          
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static List<Poll> Poll_List(int PollID, int Published)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Poll_List");
            db.AddInParameter(dbCommand, "PollID", DbType.Int32, PollID);
            db.AddInParameter(dbCommand, "Published", DbType.Int32, Published);
            List<Poll> listObj = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    (listObj = new List<Poll>()).Add(LoadObj(dataReader));
                    while (dataReader.Read())
                        listObj.Add(LoadObj(dataReader));
                }
            }
            return listObj;
        }
    }
}
